package org.mcb.zebra.portal.tag;

import java.io.IOException;

import javax.portlet.PortletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.pluto.PortletContainer;
import org.apache.pluto.PortletContainerException;
import org.apache.pluto.PortletWindow;
import org.mcb.zebra.portal.manager.PortalManager;

@SuppressWarnings("serial")
public class PortletWindowTag extends TagSupport{
	private String context;
	private String portletName;
	private PortletWindow portletWindow = null;
	public String getContext() {
		return context;
	}
	public void setContext(String context) {
		this.context = context;
	}
	public String getPortletName() {
		return portletName;
	}
	public void setPortletName(String portletName) {
		this.portletName = portletName;
	}
	@Override
	public int doStartTag() throws JspException {
		ServletContext context =pageContext.getServletContext();
		PortletContainer container =(PortletContainer) context.getAttribute(PortalManager.PORTLET_CONTAINER);
		PortalManager portalManager = (PortalManager) context.getAttribute(PortalManager.PORTAL_MANAGER);
		if(container!=null){
			if(portletWindow==null){
				portletWindow = portalManager.getPortletWindow(this.context, portletName);
			}
			try {
				container.doRender(portletWindow,(HttpServletRequest)this.pageContext.getRequest() ,(HttpServletResponse)this.pageContext.getResponse());
			} catch (PortletException e) {
				throw new JspException(e);
			} catch (IOException e) {
				throw new JspException(e);
				
			} catch (PortletContainerException e) {
				throw new JspException(e);
			}
		}
		return TagSupport.EVAL_BODY_INCLUDE;
	}
	@Override
	public int doEndTag() throws JspException {
		
		return super.doEndTag();
	}
}
